package com.boot.main.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.boot.main.entity.CcomUser;
import com.boot.main.utils.IsNotEmptyUtil;
import com.boot.main.utils.StringUtil;
import com.boot.main.config.configDemo.MyLog;
import com.boot.main.config.redis.JedisUtil;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IUserService;
import io.swagger.annotations.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/v1/api")
@Api(value = "登录", tags = "登录模块", description = "登录Rest API")
public class LoginController {
    @Resource
    private IUserService userService;

    @RequestMapping(value="/login", method = RequestMethod.POST)
    @ApiOperation(value = "登录", notes="用户名密码不能为空")
    @MyLog(value = "用户登录")
    public Object login(@RequestBody Map<String, Object> map, HttpServletResponse response, HttpServletRequest request){
        String userName = map.get("username").toString();
        String passWord = map.get("password").toString();
        String captcha = map.get("captcha").toString();
        String codeNull = String.valueOf(request.getSession().getAttribute("code"));
        String code = JedisUtil.getJson("code");
        if(!captcha.equals(code)){
            return ResponseDataUtil.error("登录失败，验证码不正确！", null);
        }
        if(StringUtil.isNotBlank(userName) && StringUtil.isNotBlank(passWord)){
            return userService.login(userName, passWord, response);
        }else{
            log.info("登录失败，用户名或密码为空！");
            return ResponseDataUtil.error("登录失败，用户名或密码为空！", null);
        }
    }

    @RequestMapping(value="/login/{type}", method = RequestMethod.POST)
    @ApiOperation(value = "第三方登录")
    public Object otherLogin(@RequestBody CcomUser ccomUser, @PathVariable Integer type, HttpServletResponse response){
        String userName = ccomUser.getUsername();
        if(IsNotEmptyUtil.isEmpty(userName)){
            return ResponseDataUtil.error("手机号码不能为空!", null);
        }
        return userService.otherLogin(ccomUser, type, response);
    }


    @ApiOperation(value="登出")
    @RequestMapping(value="/logout", method = RequestMethod.POST)
    @MyLog(value = "用户登出")
    public Object logout(HttpServletRequest request){
        return userService.logout(request);
    }

    @ApiIgnore
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "username", value = "用户名", required = true),
//            @ApiImplicitParam(name = "password", value = "密码", required = true)
//    })
    @RequestMapping(value="/loginAction", method = RequestMethod.GET)
    public Object loginAction(
//            @ApiParam(name="password",value="密码",required=true) String password,
//            @ApiParam(name="username",value="用户名") String username,
            @RequestParam("username") String username,
            @RequestParam("password") String password, HttpServletResponse response){
        if(StringUtil.isNotBlank(username) && StringUtil.isNotBlank(password)){
            return userService.login(username, password, response);
        }else{
            log.info("登录失败，用户名或密码为空！");
            return ResponseDataUtil.error( "登录失败，用户名或密码为空！", null);
        }
    }

    @ApiIgnore
    @SneakyThrows
    @RequestMapping(value="/code", method = RequestMethod.GET)
    @ApiOperation(value="验证码")
    public void getCode(HttpServletResponse response, HttpServletRequest request){
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 80, 4, 5);
        String code = captcha.getCode();
        request.getSession().setAttribute("code", code);
        JedisUtil.setJson("code", code, 3600*1000);
        captcha.write(response.getOutputStream());
    }
}
